//
//  LoginSegmentView.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/12.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "LoginSegmentView.h"

typedef NS_ENUM(NSInteger,LoginSegmentViewButton){
    LoginSegmentViewButtonLeft,
    LoginSegmentViewButtonRight
};

@interface LoginSegmentView()
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIButton *registerBtn;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) NSMutableArray *buttonsArray;



@end


@implementation LoginSegmentView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self loadSubviewsOnSegment];
        // 根据scrollview的滑动转换
        [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"ChangeNavIndex" object:nil] subscribeNext:^(NSNotification *x) {
            
            // 滑动之后的方法
            [self scrollToCurrentWithIndex:[x.object[@"index"] integerValue]];
        }];
    }
    return self;
}

// loadSubviews
- (void)loadSubviewsOnSegment{
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    backgroundView.backgroundColor = KLColor_C1;
    self.backgroundView = backgroundView;
    [self addSubview:backgroundView];
    // 注册
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    registerBtn.frame = CGRectMake(0, 0, self.width/2.0f, self.height);
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    registerBtn.tag = LoginSegmentViewButtonLeft;
    registerBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15*WIDTH];
    registerBtn.backgroundColor = KLColor_A1;
    [registerBtn addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:registerBtn];
    self.registerBtn = registerBtn;
    [self.buttonsArray addObject:self.registerBtn];
    
    // 登录
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame = CGRectMake(self.width/2.0, 0, self.width/2.0f, self.height);
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:KLColor_B5 forState:UIControlStateNormal];
    loginBtn.tag = LoginSegmentViewButtonRight;
    loginBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15*WIDTH];
    loginBtn.backgroundColor = KLColor_C1;
    [loginBtn addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:loginBtn];
    self.loginBtn = loginBtn;
    [self.buttonsArray addObject:self.loginBtn];
}

// 圆角
- (void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];
    [self.backgroundView layoutIfNeeded];
    [self.backgroundView setRadiusWithValue:self.height/2.0f];
    [self.registerBtn layoutIfNeeded];
    [self.registerBtn setRadiusWithValue:self.height/2.0f];
    [self.loginBtn layoutIfNeeded];
    [self.loginBtn setRadiusWithValue:self.height/2.0f];
}


// lazyload
- (NSMutableArray *)buttonsArray{
    if (!_buttonsArray) {
        _buttonsArray = [NSMutableArray array];
    }
    return _buttonsArray;
}

// buttonclick
- (void)selectClick:(UIButton *)sender{
    [self filterButtonChange];
    sender.selected = YES;
    sender.backgroundColor = KLColor_A1;
    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if (self.delegateSignal) {
        [self.delegateSignal sendNext:@{@"index":@(sender.tag)}];
    }
}

// 滑动的方法
- (void)scrollToCurrentWithIndex:(NSInteger)index{
    [self filterButtonChange];
    UIButton *tempButton = self.buttonsArray[index];
    tempButton.selected = YES;
    tempButton.backgroundColor = KLColor_A1;
    [tempButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

// 过滤
- (void)filterButtonChange{
    [self.buttonsArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = NO;
        obj.backgroundColor = KLColor_C1;
        [obj setTitleColor:KLColor_B5 forState:UIControlStateNormal];
    }];

}

@end
